package com.example.photography.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.core.widget.NestedScrollView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.Activity.Appointment_Actvity;
import com.example.photography.Activity.Edit.Edit_Information;
import com.example.photography.Activity.Inform_Activity;
import com.example.photography.Activity.Order_photo;
import com.example.photography.Activity.Setting_Activity;
import com.example.photography.Activity.me.Focus_Activity;
import com.example.photography.Activity.me.Product_Activity;
import com.example.photography.R;
import com.example.photography.bean.MefragmentBean;
import com.example.photography.common.BaseFragment;
import com.example.photography.ui.CircleImageView;
import com.example.photography.util.AppBarStateChangeListener;
import com.example.photography.util.UIUtils;
import com.example.photography.view.Message_Dialog;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.loopj.android.http.RequestParams;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.RoundLinesIndicator;
import com.youth.banner.util.BannerUtils;

import java.util.Objects;

import butterknife.BindView;

import static com.example.photography.Activity.Store_Activity.getJson;

public class MeFragment extends BaseFragment {


    @BindView(R.id.cv_head)
    CircleImageView cvHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.iv_sex)
    ImageView ivSex;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.tv_attention)
    TextView tvAttention;
    @BindView(R.id.ll_attention)
    LinearLayout llAttention;
    @BindView(R.id.tv_fans)
    TextView tvFans;
    @BindView(R.id.ll_fans)
    LinearLayout llFans;
    @BindView(R.id.tv_works)
    TextView tvWorks;
    @BindView(R.id.ll_works)
    LinearLayout llWorks;
    @BindView(R.id.tv_like)
    TextView tvLike;
    @BindView(R.id.ll_like)
    LinearLayout llLike;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.tl_sign)
    ImageView tlSign;
    @BindView(R.id.tl_name)
    TextView tlName;
    @BindView(R.id.tl_scan)
    ImageView tlScan;
    @BindView(R.id.tl_share)
    ImageView tlShare;
    @BindView(R.id.tl_setting)
    ImageView tlSetting;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbarlayot)
    AppBarLayout appbarlayot;
    @BindView(R.id.banner)
    Banner banner;

    @BindView(R.id.bt_apply)
    Button btApply;
    @BindView(R.id.ns_view)
    NestedScrollView nsView;
    @BindView(R.id.tv_appointment)
    TextView tvAppointment;
    @BindView(R.id.iv_authentication)
    ImageView ivAuthentication;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_product)
    TextView tvProduct;
    private MefragmentBean mefragmentBean;
    private Message_Dialog mMyDialog;
    private BottomSheetDialog sheelt;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    protected void initData(String content) {
        String json = getJson(Objects.requireNonNull(getActivity()), "me_data.json");
        mefragmentBean = JSONObject.parseObject(json, MefragmentBean.class);
        initView();
        initListen();

    }

    private void initView() {
        //toolbar
        tlName.setText(mefragmentBean.getUserInfo().getName());

        /**
         * CollapsingToolbarLayout
         */
        //rl_head
        tvName.setText(mefragmentBean.getUserInfo().getName());
        tvSign.setText(mefragmentBean.getUserInfo().getSign());
        Glide.with(Objects.requireNonNull(getActivity()))
                .load(mefragmentBean.getUserInfo().getHead_url())
                .thumbnail(Glide.with(UIUtils.getContext()).load(R.drawable.loading))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(cvHead);
        //
        tvAttention.setText(mefragmentBean.getState().getAttention_num());
        tvFans.setText(mefragmentBean.getState().getFans_num());
        tvWorks.setText(mefragmentBean.getState().getLabel_num());
        tvLike.setText(mefragmentBean.getState().getHost_num());
        /**
         * banner
         */

        banner.setAdapter(new BannerImageAdapter<MefragmentBean.BannerBean>(mefragmentBean.getBanner()) {

            @Override
            public void onBindView(BannerImageHolder holder, MefragmentBean.BannerBean data, int position, int size) {
                //图片加载自己实现
                Glide.with(holder.itemView)
                        .load(data.getImage())
                        .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        });
        banner.setIndicator(new RoundLinesIndicator(getActivity()));
        banner.setIndicatorSelectedWidth((int) BannerUtils.dp2px(15));


    }

    private void initListen() {
        //监听折叠
        appbarlayot.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                /**
                 * State.COLLAPSED:展开状态
                 * State.COLLAPSED:折叠状态
                 * 中间状态用else判断
                 */
                if (state == State.COLLAPSED) {
                    tlName.setVisibility(View.VISIBLE);
                } else {
                    tlName.setVisibility(View.GONE);
                }

            }
        });

        tlSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Setting_Activity.class);
                startActivity(intent);
            }
        });

        tlScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(getActivity(), Interest_choose.class);
//                startActivity(intent);
                Intent intent = new Intent(getActivity(), Inform_Activity.class);
                startActivity(intent);
            }
        });

        tlShare.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                sheelt = new BottomSheetDialog(getActivity());
                View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.bottom_share, null);
                sheelt.setContentView(contentView);
                //给布局设置透明背景色
                sheelt.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
                sheelt.show();
                TextView iv_dismiss = contentView.findViewById(R.id.tv_dismiss);
                iv_dismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sheelt.dismiss();//消失
                    }
                });
            }
        });
        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Edit_Information.class);
                startActivity(intent);
            }
        });
        llAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Focus_Activity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 0);
                intent.putExtra("data", bundle);
                startActivity(intent);
            }
        });
        llFans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Focus_Activity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 1);
                intent.putExtra("data", bundle);
                startActivity(intent);
            }
        });
        llLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View message_dialog = getLayoutInflater().inflate(R.layout.message_dialog_like, null);
                mMyDialog = new Message_Dialog(getActivity(), 0, 0, message_dialog, R.style.DialogTheme);
                mMyDialog.setCancelable(true);
                TextView tv_title = message_dialog.findViewById(R.id.tv_title);
                TextView tv_publish = message_dialog.findViewById(R.id.tv_publish);
                TextView tv_like = message_dialog.findViewById(R.id.tv_like);
                TextView tv_collect = message_dialog.findViewById(R.id.tv_collect);
                TextView tv_publish_num = message_dialog.findViewById(R.id.tv_publish_num);
                TextView tv_like_num = message_dialog.findViewById(R.id.tv_like_num);
                TextView tv_collect_num = message_dialog.findViewById(R.id.tv_collect_num);
                TextView tv_ok = message_dialog.findViewById(R.id.tv_ok);
                tv_title.setText("获 赞 与 收 藏");
                tv_publish.setText("当前发布作品数");
                tv_like.setText("当前获得点赞数");
                tv_collect.setText("当前获得收藏数");
                tv_publish_num.setText("10");
                tv_like_num.setText("70");
                tv_collect_num.setText("20");
                mMyDialog.show();
                tv_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mMyDialog.dismiss();
                    }
                });
            }
        });
        llWorks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Product_Activity.class);
                startActivity(intent);
            }
        });
        tvAppointment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Appointment_Actvity.class);
                startActivity(intent);
            }
        });
        tvOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Order_photo.class);
                startActivity(intent);
            }
        });
        tvProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Product_Activity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //防止窗体泄漏
        if (mMyDialog != null) {
            mMyDialog.dismiss();
        } else if (sheelt != null) {
            sheelt.dismiss();
        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }
}
